package com.frank.music;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import com.frank.demo.util.Encodes;
import com.frank.demo.util.RSAUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.fluent.Request;
import org.apache.http.message.BasicNameValuePair;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.junit.Test;
import sun.misc.Unsafe;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPublicKeySpec;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhy on 2018/8/3.
 */
public class Comments {


    @Test
    public void run() throws Exception {
        String songUrl = "https://music.163.com/weapi/v2/discovery/recommend/songs";
//        String commentUrl = "https://music.163.com/weapi/v1/resource/comments/R_SO_4_387635?csrf_token=";
        String songParams = songParams();
        String commentParams = comentsParams();

//        System.out.println(songParams);

        System.out.println(getMusic(songUrl, songParams));
//        System.out.println(getMusic(commentUrl, commentParams));
    }

    public String getMusic(String url, String params) throws IOException {

//        HttpHost proxy = new HttpHost("111.155.116.236", 8123, "http");

        params = "5b00A489lqiYijFdh5f0fniP1x2ZZ/vh6BbtNdaKrgbKe0pJYvBlYzBxS3tU6wHIA4i2J7Ujm/MK5PGpyICC+13v/PKTmYnGbdDzxHAzr08+YLuu5J88HMub8YWhtgn3lw6V8vBxsXx9Juaj5x3cmBWnODpFjddcnSYSVCPMHM1HIyOryYfMzOBx40c+O9BT";
        String encSecKey = "bf50d0bcf56833b06d8d1219496a452a1d860fd58a14c0aafba3e770104ca77dc6856cb310ed3309039e6865081be4ddc2df52663373b20b70ac25b4d0c6ca466daef6b50174e93536e2d580c49e70649ad1936584899e85722eb83ceddfb4f56c1172fca5e60592d0e6ee3e8e02be1fe6e53f285b0389162d8e6ddc553857cd";


        List<NameValuePair> postParameters = new ArrayList<>();
        // 添加属性对
        postParameters.add(new BasicNameValuePair("encSecKey", encSecKey));
        postParameters.add(new BasicNameValuePair("params", params));

        String res = Request.Post(url)
//                .viaProxy(proxy)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36")
                .setHeader("Cookie", " MUSIC_U=03aef0d0c5dbf9c5617669c3431c25f4da054a7a0c40299be9516cbbd6b9d8c3d0817174dd864582c9ed344f26f36d5e6050965ae19ab49cf2d31be995218b9b")
                .setHeader("Host", "music.163.com")
                .body(new UrlEncodedFormEntity(postParameters))
                .execute()
                .returnContent()
                .asString();
        return res;
    }

    private String songParams() {
        //5b00A489lqiYijFdh5f0fniP1x2ZZ/vh6BbtNdaKrgbKe0pJYvBlYzBxS3tU6wHIA4i2J7Ujm/MK5PGpyICC+w51xywYH63HiTk5fuT8vrBbMQ87AI0WUa67uNUZqduJEpa8DdylvwyPNyeoJPqOWHUcodcACXBetpYIY7M1LwCL0shyT+TjopCm3V4Ru3sh
        String encodeStr = "{\"offset\":\"0\",\"total\":\"true\",\"limit\":\"20\",\"csrf_token\":\"e81c01fed7babb0d1a8e8a553fc1113e\"}";
        byte[] b = Encodes.encryptAESDefault(encodeStr.getBytes(), "0CoJUm6Qyw8W8jud".getBytes(), "0102030405060708");
        byte[] c = Encodes.encryptAESDefault(Encodes.encodeBase64(b).getBytes(), "0CoJUm6Qyw8W8jud".getBytes(), "0102030405060708");
        return Encodes.encodeBase64(c);
    }

    private String comentsParams() throws UnsupportedEncodingException {
//        byte[] b = Encodes.decodeBase64("tYunZCdMmGa33OrU4PZgBGpQr/r+b58+M4QVlrmRYeQqgW4Ngh9HfekQhmETWv2n/zgF5c3GwI/WKunulrcVQZb0CSRI93EF9MIDfbe0kIfbh9GrmOyQcKzzlEi3ohIJ");
        String encodeStr = "{\"rid\":\"R_SO_4_387635\",\"offset\":\"0\",\"total\":\"false\",\"limit\":\"20\",\"csrf_token\":\"\"}";

        byte[] b = Encodes.encryptAESDefault(encodeStr.getBytes(), "0CoJUm6Qyw8W8jud".getBytes(), "0102030405060708");
        byte[] c = Encodes.encryptAESDefault(Encodes.encodeBase64(b).getBytes(), "0CoJUm6Qyw8W8jud".getBytes(), "0102030405060708");
//        System.out.println(Encodes.encodeBase64(c));
        return Encodes.encodeBase64(c);
    }

    @Test
    public void RSA() throws Exception {
        String after = "bf50d0bcf56833b06d8d1219496a452a1d860fd58a14c0aafba3e770104ca77dc6856cb310ed3309039e6865081be4ddc2df52663373b20b70ac25b4d0c6ca466daef6b50174e93536e2d580c49e70649ad1936584899e85722eb83ceddfb4f56c1172fca5e60592d0e6ee3e8e02be1fe6e53f285b0389162d8e6ddc553857cd";

        KeyFactory keyFactory = KeyFactory.getInstance("RSA", new BouncyCastleProvider());
        System.out.println(new BigInteger(Encodes.decodeHex("00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7")) + "|||" + new BigInteger(Encodes.decodeHex("010001")));
        RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger(Encodes.decodeHex("00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7")), new BigInteger(Encodes.decodeHex("010001")));
        byte[] encode = RSAUtils.encrypt((RSAPublicKey) keyFactory.generatePublic(pubKeySpec), new StringBuilder("0CoJUm6Qyw8W8jud").toString().getBytes());
        System.out.println(Encodes.encodeHex(encode));
    }

    @Test
    public void RSA2() throws NoSuchAlgorithmException, InvalidKeySpecException {
        RSA rsa = new RSA();

        KeyFactory keyFactory = KeyFactory.getInstance("RSA", new BouncyCastleProvider());
        RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger(Encodes.decodeHex("00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7")), new BigInteger("010001"));
        rsa.setPublicKey(keyFactory.generatePublic(pubKeySpec));

        //公钥加密，私钥解密
        byte[] encrypt = rsa.encrypt(StrUtil.bytes("0CoJUm6Qyw8W8jud", CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
        System.out.println(Encodes.encodeHex(encrypt));
    }
}
